﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Dycox.ComponentModel;

namespace Dycox.Windows.Forms
{
    public class DetailsViewPropertyItem : DetailsViewItem
    {
        public DetailsViewPropertyItem(PropertyDescriptor property, object instance)
            : base(property.DisplayName)
        {
            _property = property;
            _instance = instance;

            object value;

            try { value = _property.GetValue(_instance); }
            catch { value = null; }

            _value = value;

            var cvt = property.Converter;
            SimpleTypeDescriptorContext ctx = new SimpleTypeDescriptorContext(instance, property);

            if (value != null && cvt.GetPropertiesSupported(ctx))
            {
                _properties = cvt.GetProperties(ctx, value, Attributes);
            }
            else
            {
                Type type = property.PropertyType;
                if (type.IsClass && !type.IsPrimitive && type != typeof(string))
                    _properties = property.GetChildProperties(value, Attributes);
            }

            ItemText = GetValueText();
        }

        public override bool Expandable
        {
            get
            {
                return _properties != null && _properties.Count > 0 && _value != null;
            }
        }

        protected override void PopulateItems(List<DetailsViewItem> list)
        {
            foreach (PropertyDescriptor p in _properties)
            {
                list.Add(DetailsViewItem.CreateItem(p, _value));
            }
        }

        PropertyDescriptor _property;

        PropertyDescriptorCollection _properties;

        object _instance;

        private object _value;

        public object Value
        {
            get { return _value; }
        }

        protected virtual string GetValueText()
        {

            string text;
            object value = _value;
            DisplayFormatAttribute format;


            format = (DisplayFormatAttribute)_property.Attributes[typeof(DisplayFormatAttribute)];

            if (value != null)
            {
                if (value is Enum)
                    text = Dycox.EnumHelpers.GetText((Enum)value);

                else if (value is DateTime && (format == null || string.IsNullOrEmpty(format.Format)))
                {
                    DateTime v = (DateTime)value;

                    if (v != v.Date)
                        text = v.ToString("f");
                    else
                        text = v.ToLongDateString();
                }
                else
                {
                    //if (format != null)
                        text = Dycox.Utils.Formatting.ToString(value, _property);
                    //else
                    //    text = DetailsViewInstanceItem.GetObjectText(value);
                        if (text == value.GetType().FullName)
                            text = string.Empty;
                }
            }
            else
            {
                if (format != null)
                    text = format.NullText ?? string.Empty;
                else
                    text = string.Empty;
            }

            return text;
        }
    }
}
